home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / Onboard / KeyGtk.py < prev    next >
Text File  |  2009-10-01  |  7KB  |  204 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. import pango
  4.  
  5. from math import floor
  6.  
  7. from Onboard.KeyCommon import *
  8.  
  9. ### Logging ###
  10. import logging
  11. _logger = logging.getLogger("KeyGTK")
  12. ###############
  13.  
  14. ### Config Singleton ###
  15. from Onboard.Config import Config
  16. config = Config()
  17. ########################
  18.  
  19. BASE_FONTDESCRIPTION_SIZE = 10000000
  20.  
  21. class Key(KeyCommon):
  22.     def __init__(self):
  23.         KeyCommon.__init__(self)
  24.  
  25.     def get_best_font_size(self, scale, context):
  26.         """
  27.         Get the maximum font possible that would not cause the label to
  28.         overflow the boundaries of the key.
  29.         """
  30.  
  31.         raise NotImplementedException()
  32.  
  33.     def paint_font(self, scale, location, context):
  34.  
  35.         context.move_to((location[0] + self.label_offset[0]) * scale[0],
  36.                         (location[1] + self.label_offset[1]) * scale[1])
  37.  
  38.         context.set_source_rgba(self.label_rgba[0], self.label_rgba[1], 
  39.                                 self.label_rgba[2], self.label_rgba[3])        
  40.         layout = context.create_layout()
  41.         layout.set_text(self.labels[self.label_index])
  42.         font_description = pango.FontDescription()
  43.         font_description.set_size(self.font_size)
  44.         font_description.set_family("Normal")
  45.         layout.set_font_description(font_description)
  46.         context.update_layout(layout)            
  47.         context.show_layout(layout)
  48.  
  49.  
  50. class TabKey(Key, TabKeyCommon):
  51.     def __init__(self, keyboard, width, pane):
  52.         TabKeyCommon.__init__(self, keyboard, width, pane)
  53.         Key.__init__(self)
  54.  
  55.     def paint(self, context = None):
  56.         TabKeyCommon.paint(self, context)
  57.         context.rectangle(self.keyboard.kbwidth, 
  58.                           self.height * self.index + BASE_PANE_TAB_HEIGHT, self.width, self.height)
  59.  
  60.         if self.pane == self.keyboard.activePane and self.stuckOn:
  61.             context.set_source_rgba(1, 0, 0,1)
  62.         else:       
  63.             context.set_source_rgba(float(self.pane.rgba[0]), float(self.pane.rgba[1]),float(self.pane.rgba[2]),float(self.pane.rgba[3]))
  64.         
  65.         context.fill()
  66.  
  67.     
  68. class BaseTabKey(Key, BaseTabKeyCommon):
  69.     def __init__(self, keyboard, width):
  70.         BaseTabKeyCommon.__init__(self, keyboard, width)
  71.         Key.__init__(self)
  72.  
  73.     ''' this class has no UI-specific code at all. Why? '''
  74.     def paint(self,context):
  75.         #We don't paint anything here because we want it to look like the base pane.
  76.         pass
  77.  
  78. class LineKey(Key, LineKeyCommon):
  79.     def __init__(self, name, coordList, fontCoord, rgba):
  80.         LineKeyCommon.__init__(self, name, coordList, fontCoord, rgba)
  81.         Key.__init__(self)
  82.  
  83.     def point_within_key(self, location, scale, context):
  84.         """Cairo specific, hopefully fast way of doing this"""
  85.  
  86.         context = widget.window.cairo_create()
  87.         self.draw_path(scale[0], scale[1], context)
  88.         return context.in_fill(location[0], location[1])
  89.  
  90.     def paint(self, scale, context):
  91.         self.draw_path(scale, context)
  92.  
  93.         if (self.stuckOn):
  94.             context.set_source_rgba(1.0, 0.0, 0.0,1.0)
  95.         elif (self.on):
  96.             context.set_source_rgba(0.5, 0.5, 0.5,1.0)
  97.         elif (self.beingScanned):   
  98.             context.set_source_rgba(0.45,0.45,0.7,1.0)
  99.         else:
  100.             context.set_source_rgba(self.rgba[0], self.rgba[1],self.rgba[2],self.rgba[3])
  101.  
  102.         context.fill_preserve()
  103.         context.set_source_rgb(0, 0, 0)
  104.         context.stroke()
  105.  
  106.     def draw_path(self, scale, context):
  107.         ''' currently this method contains all the LineKey 
  108.             painting code.┬á'''
  109.  
  110.         LineKeyCommon.paint(self, scale, context = None)
  111.         c = 2
  112.         context.move_to(self.coordList[0] * scale[0], 
  113.                         self.coordList[1] * scale[1])
  114.  
  115.         while not c == len(self.coordList):
  116.             xp1 = self.coordList[c+1] * scale[0]
  117.             yp1 = self.coordList[c+2] * scale[1]
  118.             try:
  119.                 if self.coordList[c] == "L":
  120.                     c +=3
  121.                     context.line_to(xp1,yp1)
  122.                 else:   
  123.                     xp2 = self.coordList[c+3] * scale[0]
  124.                     yp2 = self.coordList[c+4] * scale[1]
  125.                     xp3 = self.coordList[c+5] * scale[0]
  126.                     yp3 = self.coordList[c+6] * scale[1]
  127.                     context.curve_to(xp1,yp1,xp2,yp2,xp3,yp3)
  128.                     c += 7
  129.  
  130.             except TypeError, (strerror):
  131.                 print yp1
  132.                 print strerror
  133.  
  134.                 
  135.  
  136.     def paint_font(self, scale, context = None):
  137.         Key.paint_font(self, scale, self.fontCoord, context)
  138.  
  139.  
  140.     
  141. class RectKey(Key, RectKeyCommon):
  142.     def __init__(self, name, location, geometry, rgba):
  143.         RectKeyCommon.__init__(self, name, location, geometry, rgba)
  144.  
  145.     def point_within_key(self, location, scale, context):
  146.         return RectKeyCommon.point_within_key(self, location, scale)
  147.  
  148.     def paint(self, scale, context = None):
  149.         
  150.         context.rectangle(self.location[0] * scale[0],
  151.                           self.location[1] * scale[1],
  152.                           self.geometry[0] * scale[0],
  153.                           self.geometry[1] * scale[1])
  154.         
  155.         if (self.stuckOn):
  156.             context.set_source_rgba(1, 0, 0,1)
  157.         elif (self.on):
  158.             context.set_source_rgba(0.5, 0.5, 0.5,1)
  159.         elif (self.beingScanned):   
  160.             context.set_source_rgba(0.45,0.45,0.7,1)
  161.         else:
  162.             context.set_source_rgba(self.rgba[0], self.rgba[1],self.rgba[2],self.rgba[3])
  163.         
  164.         context.fill_preserve()
  165.         context.set_source_rgb(0, 0, 0)
  166.         context.stroke()
  167.  
  168.     def paint_font(self, scale, context = None):
  169.         Key.paint_font(self, scale, self.location, context)
  170.  
  171.     def get_best_font_size(self, scale, context):
  172.         """
  173.         Get the maximum font possible that would not cause the label to
  174.         overflow the boundaries of the key.
  175.         """
  176.  
  177.         layout = pango.Layout(context)
  178.         layout.set_text(self.labels[self.label_index])
  179.         font_description = pango.FontDescription()
  180.         font_description.set_size(BASE_FONTDESCRIPTION_SIZE)
  181.         font_description.set_family("Normal")
  182.         layout.set_font_description(font_description)
  183.  
  184.         # In Pango units
  185.         label_width, label_height = layout.get_size()
  186.         
  187.         size_for_maximum_width = (self.geometry[0] - config.LABEL_MARGIN[0]) \
  188.                 * pango.SCALE \
  189.                 * scale[0] \
  190.                 * BASE_FONTDESCRIPTION_SIZE \
  191.             / label_width
  192.  
  193.         size_for_maximum_height = (self.geometry[1] - config.LABEL_MARGIN[1]) \
  194.                 * pango.SCALE \
  195.                 * scale[1] \
  196.                 * BASE_FONTDESCRIPTION_SIZE \
  197.             / label_height
  198.  
  199.         if size_for_maximum_width < size_for_maximum_height:
  200.             return int(floor(size_for_maximum_width))
  201.         else:
  202.             return int(floor(size_for_maximum_height))
  203.         
  204.